﻿using System;
using Database;
using System.Data;

namespace ObjectManager
{
    class MonHocDAO : IDatabase
    {
        private ConnectionDB cnn = null;

        public MonHocDTO MonHocDTO { get; set; }

        public MonHocDAO()
        {
            cnn = Connection.Connect();
            this.MonHocDTO = new MonHocDTO();
        }

        private int MaBoMon()
        {
            DataTable temp = cnn.QueryProcedure("TimMaBoMon", MonHocDTO.BoMon);
            int MaBM = 0;
            if (temp != null && temp.Rows.Count > 0 && !string.IsNullOrEmpty(temp.Rows[0][0].ToString()))
            {
                MaBM = (int)temp.Rows[0][0];
            }
            return MaBM;
        }

        public int Insert()
        {
            if (!string.IsNullOrEmpty(MonHocDTO.Ten))
            {
                return cnn.ExecuteProcedure("InsertMonHoc_HSDiemQT", MonHocDTO.Ten, MonHocDTO.SoTinChi, MonHocDTO.HeSoDiemQT, MaBoMon(), MonHocDTO.GhiChu);
            }
            return -1;
        }

        public int Update()
        {
            if (!string.IsNullOrEmpty(MonHocDTO.Ten))
            {
                return cnn.ExecuteProcedure("UpdateMonHoc", MonHocDTO.Ma, MonHocDTO.Ten, MonHocDTO.SoTinChi, MonHocDTO.HeSoDiemQT, MaBoMon(), MonHocDTO.GhiChu);
            }
            return -1;
        }

        public int Delete()
        {
            if (!string.IsNullOrEmpty(MonHocDTO.Ten))
            {
                return cnn.ExecuteProcedure("DeleteMonHoc_Ten", MonHocDTO.Ten);
            }
            return -1;
        }

        public DataTable List(int Ma)
        {
            return cnn.QueryProcedure("DanhSachMonHoc_Ma", Ma);
        }

        public DataTable List(string Ten, string BoMon)
        {
            return cnn.QueryProcedure("DanhSachMonHoc_TenBoMon", Ten, BoMon);
        }

        public DataTable ListAll()
        {
            return cnn.QueryProcedure("DanhSachMonHoc");
        }

        public DataTable ListName()
        {
            return cnn.Query("select Ma, Ten, SoTC from vw_MonHoc where 1=2;");
        }

        public string[] FindName()
        {
            string[] result = new string[7];
            for (int i = 0; i < 7; i++)
            {
                result[i] = "";
            }
            DataTable temp = cnn.QueryProcedure("TimTenMonHoc", MonHocDTO.Ma);
            if (temp != null && temp.Rows.Count > 0 && !string.IsNullOrEmpty(temp.Rows[0][0].ToString()))
            {
                for (int i = 0; i < 7; i++)
                {
                    result[i] = temp.Rows[0][i].ToString();
                }
            }
            return result;
        }

        public int MaTiep()
        {
            int result = 0;
            DataTable temp = cnn.QueryProcedure("TimMaMonHocLonNhat");
            if (temp != null && temp.Rows.Count > 0 && !string.IsNullOrEmpty(temp.Rows[0][0].ToString()))
            {
                result = int.Parse(temp.Rows[0][0].ToString()) + 1;
            }
            return result;
        }

        public void Search(int Ma)
        {
            DataTable temp = cnn.Query("select * from vw_MonHoc where Ma = {0}", Ma);
            if (temp != null && temp.Rows.Count > 0)
            {
                this.MonHocDTO.Ma = temp.Rows[0][0].ToString();
                this.MonHocDTO.Ten = temp.Rows[0][1].ToString();
                this.MonHocDTO.SoTinChi = int.Parse(temp.Rows[0][2].ToString());
                this.MonHocDTO.HeSoDiemQT = temp.Rows[0][3].ToString();
                this.MonHocDTO.BoMon = temp.Rows[0][5].ToString();
                this.MonHocDTO.GhiChu = temp.Rows[0][6].ToString();
            }
        }
    }
}
